/**
 * 
 */
package com.xcn.socket;

import java.io.*;
import java.net.*;

import kr.co.xcurenet.crypt.*;
import net.sf.json.*;

/**
 * @author changmin
 *
 */
public class ClientDaemon
{
	
	/**
	 * 
	 */
	public ClientDaemon( )
	{
	}
	
	
	
	public static String decryptionMsg( DataInputStream dis )
	{
		ByteArrayOutputStream _bao = new ByteArrayOutputStream ( );
		byte[] key = new byte[] {'7', 'T', 'Q', 'F', '1', 'N', '4', 'U', 'K', 'X', 'O', 'K', 'D', 'N', 'N', 'G'};
		try
		{
			ARIA _aria = new ARIA ( key );
			_aria.decode ( dis, _bao );
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
		return _bao.toString ( );
	}
	
	
	public String getMessage ( String ip, int port, String command ) throws UnknownHostException, IOException
	{
		String resultMessage = "";
		Socket _socket = new Socket ( ip, port );
		if ( _socket.isConnected( ) )
		{
			DataOutputStream dos = new DataOutputStream ( new BufferedOutputStream ( _socket.getOutputStream ( ) ) );
			DataInputStream dis = new DataInputStream ( new BufferedInputStream ( _socket.getInputStream ( ) ) );

			dos.writeUTF ( command );
			dos.flush( );
			try
			{
				resultMessage = decryptionMsg( dis );
			}
			catch ( Exception e )
			{
				e.printStackTrace ( );
			}
			finally
			{
				try{ if ( dis != null ) dis.close ( ); } catch (Exception e) { e.printStackTrace ( ); };
				try{ if ( dos != null ) dos.close ( ); } catch (Exception e) { e.printStackTrace ( ); };
				_socket.close ( );
			}
		}
		return resultMessage;
	}
	
	/**
	 * CPU USER 항목만 조회
	 * @param ip
	 * @param port
	 * @return
	 * @throws UnknownHostException
	 * @throws IOException
	 */
	public static String getCPU( String ip, int port ) throws UnknownHostException, IOException
	{
		String cpuRate = "";

		ClientDaemon _cm = new ClientDaemon ( );
		String message = _cm.getMessage ( ip, port, "top -b -n1 -d5" );
		if ( message.length ( ) > 0 )
		{
			String[] rows = message.split ( "\n" );
			for ( int i = 0 ; i < rows.length ; i++ )
			{
				if ( i == 2 )
				{
					String[] cols = rows[i].split ( "%" );
					String[] cpu = cols[0].split ( ":" );
					
					cpuRate = cpu[1].trim ( );
				}
				if ( i > 2 ) break;
			}
		}
		return cpuRate;
	}
	
	public static JSONArray getHDD( String ip, int port ) throws UnknownHostException, IOException
	{
		JSONArray _result = new JSONArray ( );
		
		ClientDaemon _cm = new ClientDaemon ( );
		String message = _cm.getMessage ( ip, port, "df -k" );
		if ( message.length ( ) > 0 )
		{
			String[] rows = message.split ( "\n" );
			for ( int i = 0 ; i < rows.length ; i++ )
			{
				if ( i == 0 ) continue; //header

				JSONObject _obj = new JSONObject ( );
				String[] cols = rows[i].split ( " " );
				for ( int j = 0, idx=0 ; j < cols.length ; j++ )
				{
					if ( cols[j].trim ( ).length ( ) == 0 ) continue;
					if ( idx == 0 ) _obj.put ( "FILE_SYSTEM", cols[j] );
					if ( idx == 1 ) _obj.put ( "BLOCKS", cols[j] );
					if ( idx == 2 ) _obj.put ( "USED", cols[j] );
					if ( idx == 3 ) _obj.put ( "AVAILABLE", cols[j] );
					if ( idx == 4 ) _obj.put ( "USE", cols[j] );
					if ( idx == 5 ) _obj.put ( "MOUNTED", cols[j] );

					idx++;
				}
				_result.add ( _obj );
			}
		}
		return _result;
	}
	
	
	public static JSONArray getMemory( String ip, int port ) throws UnknownHostException, IOException
	{
		JSONArray _result = new JSONArray ( );
		
		ClientDaemon _cm = new ClientDaemon ( );
		String message = _cm.getMessage ( ip, port, "free -m" );
		if ( message.length ( ) > 0 )
		{
			String[] rows = message.split ( "\n" );
			for ( int i = 0, rowIdx=1 ; i < rows.length ; i++ )
			{
				if ( i == 0 ) continue; //header

				JSONObject _obj = new JSONObject ( );
				if ( rowIdx == 1 )
				{
					String row = rows[i].replaceAll ( "Mem:", "" );
					String[] cols = row.split ( " " );
					for ( int j = 0, idx=0 ; j < cols.length ; j++ )
					{
						if ( cols[j].trim ( ).length ( ) == 0 ) continue;
						if ( idx == 0 ) _obj.put ( "TOTAL", cols[j] );
						if ( idx == 1 ) _obj.put ( "USED", cols[j] );
						if ( idx == 2 ) _obj.put ( "FREE", cols[j] );
						if ( idx == 3 ) _obj.put ( "SHARED", cols[j] );
						if ( idx == 4 ) _obj.put ( "BUFFERS", cols[j] );
						if ( idx == 5 ) _obj.put ( "CACHED", cols[j] );

						idx++;
					}
				}
				if ( rowIdx == 2 )
				{
					String[] row = rows[i].split ( ":" );
					String[] cols = row[1].split ( " " );
					for ( int j = 0, idx=0 ; j < cols.length ; j++ )
					{
						if ( cols[j].trim ( ).length ( ) == 0 ) continue;
						if ( idx == 0 ) _obj.put ( "USED", cols[j] );
						if ( idx == 1 ) _obj.put ( "FREE", cols[j] );
						idx++;
					}
				}
				if ( rowIdx == 3 )
				{
					String row = rows[i].replaceAll ( "Swap:", "" );
					String[] cols = row.split ( " " );
					for ( int j = 0, idx=0 ; j < cols.length ; j++ )
					{
						if ( cols[j].trim ( ).length ( ) == 0 ) continue;
						if ( idx == 0 ) _obj.put ( "TOTAL", cols[j] );
						if ( idx == 1 ) _obj.put ( "USED", cols[j] );
						if ( idx == 2 ) _obj.put ( "FREE", cols[j] );
						idx++;
					}
				}
				_result.add ( _obj );
				rowIdx++;
			}
		}
		return _result;
	}
	
	
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public static void main ( String[] args ) throws UnknownHostException, IOException
	{
		String ipAddress = "15.1.2.116";
		int port = 7777;
		ClientDaemon _cm = new ClientDaemon ( );
		
		JSONArray hddInfo = _cm.getHDD ( ipAddress, port );
		System.out.println ( "HDD Count : " + hddInfo.size ( ) );
		for ( int i = 0 ; i < hddInfo.size ( ) ; i++ )
		{
			System.out.println ( hddInfo.getJSONObject ( i ) );
		}

		//String hddInfo = _cm.getCPU ( ipAddress, port );
		//System.out.println ( hddInfo );
	}
	
}
